﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;
using System.Text.RegularExpressions;
using Portal.CMS;


namespace Portal.CDSP
{
    public class LastestNewsWP : WebPart
    {

        public override string WebPartId
        {
            get
            {
                return "WebSite.LastestNewsWP";
            }
        }


        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string Title
        {
            get
            {
                return GetText("FTitle", "SOYT-Tin mới nhất");
            }
        }


        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    
                    new ViewCls("Left",GetText("Left","Hiển thị bên trái phía trên"),1),
                    
                   };
            }
        }

        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }


        public override string ClassName
        {
            get
            {
                return "LastestNewsWP";
            }
        }


        public override RenderInfoCls ClientRenderWebPartWithInfo(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            RenderInfoCls oRenderInfoCls = new RenderInfoCls();

            try
            {

                if (ViewName.Equals("Left"))
                {
                    oRenderInfoCls.Content = ClientRenderWebPartDefault(TabModuleId, HasPorlet, ref oRenderInfoCls.ModuleTitle);
                }
                return oRenderInfoCls;
            }
            catch (Exception ex)
            {
                oRenderInfoCls.ErrorMsg = ex.Message.ToString();
                oRenderInfoCls.Error = true;
                return oRenderInfoCls;
            }
        }

        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            return new FSpecialSubjectBLL().ReadingForServiceGroupNewsItem(belongSite, ActiveLanguage);
        }

        private string ClientRenderWebPartDefault(string TabModuleId, string HasPorlet, ref string ModuleTitle)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
            StringBuilder Html = new StringBuilder();

            try
            {
                FSpecialSubjectCls FSubjectCls = new FSpecialSubjectBLL().CreateModelItem(TabModuleCls.Id, TabModuleCls.belongSite);
                string GroupSubject = FSubjectCls.GroupNewsItemName;
                string GroupNewsItemId = FSubjectCls.GroupNewsItemId;
                string GroupNewsId = FSubjectCls.GroupNewsId;
                string belongSite = TabModuleCls.belongSite;
                string NewsByCategoryUrl = Environments.GetNewsCategoryUrl(belongSite, GroupNewsItemId, GroupSubject, "_RunTime");

                Html.Append(
                        "<div class=\"lastest-news column\">\n" +
                        "					<div class=\"lastest-news-header\">\n" +
                        "						<h2>Tin mới cập nhật</h2>\n" +
                        "					</div>\n");

                int HotNews;

                try
                {
                    HotNews = int.Parse(Environment.Environments.GetAppSettings("TopNews"));
                }
                catch { HotNews = 10; }

                DataTable dtNews = new FSpecialSubjectBLL().ReadingNews_Lastest(GroupNewsId, HotNews, belongSite);
                if (dtNews.Rows.Count == 0)
                    return Html.Append("Khong co du lieu! </div>").ToString();

                Html.Append("					<div class=\"lastest-news-content\">\n" +
                            "						<ul>\n");

                for (int iIndexNews = 0; iIndexNews < HotNews; iIndexNews++)
                {
                    string ShortId = (string)dtNews.Rows[iIndexNews]["ShortId"];

                    string Title = "";
                    try
                    {
                        Title = (string)dtNews.Rows[iIndexNews][Environments.ActiveLanguage + "Subject"];
                    }
                    catch { }

                    string Url = Environments.GetNewsUrl(belongSite, ShortId, Title, "_runtime");

                    Html.Append(
                        "		<li>\n" +
                        "			<a href=\"" + Url + "\">\n" +
                                     Title +
                        "			</a>\n" +
                        "		</li>\n"
                        );
                }
                Html.Append(
                    "</ul>" +
                    "</div>" +
                    "</div>");
                return Html.ToString();
            }
            catch (Exception ex)
            {
                return "ERROR:" + ex.Message;
            }
        }


        public string ShowTooltip(string Tooltip)
        {
            string[] charsToTrim = { "'", "[", "]", "*", "\'", ";", ",", ".", "#", "@", "%", };//!@#$%^&*()+=-[]\';,./{}|\":<>?
            for (int i = 0; i < charsToTrim.Length; i++)
            {
                if (Tooltip.IndexOf(charsToTrim[i]) != -1)
                {
                    Tooltip.Replace(charsToTrim[i], "\"" + charsToTrim[i] + "\"");
                }
            }
            return Tooltip;
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }

        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Portal.CDSP.FTopNewsWP), ActivePage);
        }
    }
}
